let http = require('http');
let fs = require('fs');
let archiver = require('archiver');
let child_process = require("child_process");

// step 1 open https://github.com/login/oauth/authorize

child_process.exec(`open https://github.com/login/oauth/authorize?client_id=Iv1.2ca49cf972b4d6a7`)

// step 2 ?

// step 3 create server, receive token click publish
// 创建 server 接收 token 后 点击发布

http.createServer(function(request, response){
    let query = querystring.parse(request.url.match(/^\/\?([\s\S]+)$/)[1]);
    publish(query.token);
}).listen(8083);

function publish(token) {
    let request = http.request({
        hostname: "127.0.0.1",
        port: 8082,
        method: "POST",
        path: "/publish?token=" + token,
        headers: {
            'Content-Type': 'application/octet-stream'
        }
    }, response => {
        console.log(response);

    });

    // let file = fs.createReadStream("./sample.html");

    const archive = archive('zip', {
        zlib: {level: 9} // Sets the compression level.
    })

    archive.directory('./sample/', false);

    archive.finalize();

    archive.pipe(request);
}

/**
 * 
 * 
 * 
 */